package com.example.jvmstudytest.outofmemory.stack;

/**
 * VM Args：-Xss2M （这时候不妨设大些，请在32位系统下运行）
 * <p>本机64位，未测试。32位下应当结果Exception in thread "main" java.lang.OutOfMemoryError: unable to create native thread
 * <p>此时的oom是因为每个线程需要2m内存作为方法栈，win32位单个进程最大2g，用完就分配不了了，就oom了
 */
public class JavaVMStackOOM {
    private void dontStop() {
        while (true) {
        }
    }
    public void stackLeakByThread() {
        while (true) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    dontStop();
                }
            });
            thread.start();
        }
    }
    public static void main(String[] args) throws Throwable {
        JavaVMStackOOM oom = new JavaVMStackOOM();
        oom.stackLeakByThread();
    }
}